package com.supergo.portal.controller;

import com.supergo.http.HttpResult;
import com.supergo.manager.feign.ApiItemCatFeign;
import com.supergo.pojo.Itemcat;
import com.supergo.user.utils.JsonUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @Classname ItemCatController
 * @Description TODO
 * @Date 2020/10/26 22:45
 * @Created by ccc-j
 * @email ccc-ju@outlook.com
 */
@RestController
public class ItemCatController {

    @Autowired
    private ApiItemCatFeign apiItemCatFeign;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @RequestMapping("/categorys/categorys")
    public HttpResult getItemCatList(){
        // 查询商品分类之前可以先查询缓存
        String json = redisTemplate.opsForValue().get("portal-itemcats");
        // 如果缓存中有直接返回
        if(StringUtils.isNotBlank(json)){
            List<Itemcat> lists = JsonUtils.jsonToList(json, Itemcat.class);
            return HttpResult.ok(lists);
        }
        // 如果缓存中没有数据，查询数据
        List<Itemcat> itemCatList = apiItemCatFeign.getItemCatList();
        // 把数据放到缓存中
        redisTemplate.opsForValue().set("portal-itemcats", JsonUtils.objectToJson(itemCatList));
        // 返回结果
        return HttpResult.ok(itemCatList);
    }

}
